package com.xblog.chatbox.vlmp.service;



import com.alibaba.fastjson2.JSONObject;
import com.xblog.chatbox.vlmp.conversation.*;
import com.xblog.chatbox.vlmp.core.message.VlmpAgentMessageBroker;
import com.xiaowu.xblog.constant.UserContext;
import com.xiaowu.xblog.model.PagerResult;
import com.xiaowu.xblog.utils.Result;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;

import java.util.List;

/**
 * @description:
 * @author: xiaowu
 * @time: 2025/8/6 0:07
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class VlmpAgentServiceImpl implements VlmpAgentService {

    private final ConversationManager conversationManager;

    private final VlmpAgentMessageBroker broker;

    @Override
    public Result<Flux<String>> completions(UserContext userContext, ConversationRequest conversationRequest) {
        Flux<String> data = broker.subscribe(userContext, conversationRequest).map(JSONObject::toJSONString);
        return Result.success(data);
    }

    @Override
    public Conversation getConversation(String conversationId) {
        return conversationManager.getConversation(conversationId);
    }

    @Override
    public List<Conversation> getConversationList(UserContext context, String topic) {
        return conversationManager.getConversationList(context, topic);
    }

    @Override
    public void delete(UserContext context, String conversationId) {
        Conversation payload = new Conversation();
        payload.setId(conversationId);
        //删除会话信息
        Conversation conversation = conversationManager.delete(context, payload);
        //同步删除人工智能平台会话内容
        broker.delete(context, conversation);
    }

    @Override
    public void rename(UserContext context, Conversation conversation) {
        conversation = conversationManager.rename(context, conversation);
        //同步重命名人工智能平台会话内容
        broker.rename(context, conversation);
    }

    @Override
    public List<Conversation> getLatestConversationList(UserContext context, String topic, int size) {
        return conversationManager.getLatestConversationList(context, topic, size);
    }

    @Override
    public PagerResult<ConversationPayload> getConversationPayloadList(UserContext context, String conversationId, String oldestPayloadId) {
        return conversationManager.getConversationPayloadList(context, conversationId, oldestPayloadId);
    }

    @Override
    public List<ConversationPayload> getLatestConversationPayloadList(UserContext context, String conversationId, int size) {
        return conversationManager.getLatestConversationPayloadList(context, conversationId, size);
    }

    @Override
    public void feedbacks(UserContext context, ConversationPayloadFeedbacks feedbacks) {
        feedbacks = conversationManager.feedbacks(feedbacks);
        //同步反馈给大模型平台
        broker.feedbacks(context, feedbacks);
    }
}
